package com.ews.onlineexchange.service;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;

@Named
public class IndexService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	EntityManager entityManager;
	private final static String GET_AUDITTED_BULLETIN_LIST_HQL =  " "
			
			+ "	SELECT b.id,b.projectName,b.bulletinReqStr,b.auditTime	"
			+ "	FROM Bulletin b				"
			+ "	WHERE b.delFlag = '0'		"
			+ "	AND b.auditResult = 1		"
			+ "	AND b.cancelled = 0			"
			+ "	ORDER BY auditTime DESC		";
	
		public List<Object[]> getAudittedBulletinList(Integer maxResults){
			if(maxResults == null) maxResults = 6;
			Query query = entityManager.createQuery(GET_AUDITTED_BULLETIN_LIST_HQL).setMaxResults(maxResults);
			return query.getResultList();
		}
		
		private final static String GET_PUBLICITY_BULLETIN_LIST_HQL =  " "
				
			+ "	SELECT b.id,b.projectName,b.bulletinReqStr,b.chooseTime		"
			+ "	FROM Bulletin b												"
			+ "	WHERE b.delFlag = '0'			"
			+ "	AND b.tradeStatus = 'Agreed'	"
			+ "	AND b.overTime >= :oneDayBefore	"
			+ "	AND b.overTime <= :now 			"
			+ "	AND b.cancelled = 0				"
			+ "	ORDER BY chooseTime DESC 		";
	
		public List<Object[]> getPublicityBulletinList(Integer maxResults){
			if(maxResults == null) maxResults = 6;
			Calendar now = Calendar.getInstance();
			Calendar oneDayBefore = Calendar.getInstance();
			oneDayBefore.set( Calendar.DAY_OF_MONTH, oneDayBefore.get( Calendar.DAY_OF_MONTH ) - 1 );
			Query query = entityManager.createQuery(GET_PUBLICITY_BULLETIN_LIST_HQL)
						.setParameter("oneDayBefore", oneDayBefore.getTime())
						.setParameter("now", now.getTime()).setMaxResults(maxResults);
			return query.getResultList();
		}
		
		private final static String GET_AGREED_BULLETIN_LIST_HQL =  " "
				
			+ "	SELECT b.id,b.projectName,b.bulletinReqStr,b.overTime	"
			+ "	FROM Bulletin b							"
			+ "	WHERE b.delFlag = '0'					"
			+ "	AND b.tradeStatus = 'Agreed'			"
			+ "	AND b.overTime < :oneDayBefore			"
			+ "	AND b.cancelled = 0						"	
			+ "	ORDER BY overTime DESC					";
		public List<Object[]> getAgreedBulletinList(Integer maxResults){
			if(maxResults == null) maxResults = 6;
			Calendar oneDayBefore = Calendar.getInstance();
			oneDayBefore.set( Calendar.DAY_OF_MONTH, oneDayBefore.get( Calendar.DAY_OF_MONTH ) - 1 );
			Query query = entityManager.createQuery(GET_AGREED_BULLETIN_LIST_HQL)
						.setParameter("oneDayBefore", oneDayBefore.getTime()).setMaxResults(maxResults);
			return query.getResultList();
		}
		
		
		public Date getPlusOneDate(Date overDate) {
			if(overDate == null) return null;
			Calendar cal = Calendar.getInstance();
			cal.setTime(overDate);
			cal.set(Calendar.DAY_OF_MONTH,cal.get(Calendar.DAY_OF_MONTH)+1);
			Date newDate = cal.getTime();
			return newDate;
		}
}
